<template>
  <div class="login">
    <img class="logo" src="../../assets/paopao.png" alt="">
    <div class="login-body login">
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="username"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field
          v-model="imgCode"
          center
          clearable
          label="验证码"
          placeholder="输入验证码"
          :rules="[{ required: true, message: '请填写验证码'}]"
        >
          <template #button>
            <t-imgCode ref="imgCodeRef" class="imgCode"/>
          </template>
        </van-field>
        <div style="margin: 16px;">
          <van-button round block color="var(--van-primary-color)" native-type="submit">登录</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script lang="ts">
import {defineComponent, reactive, ref, toRefs ,onMounted} from 'vue'
import { useStore } from 'vuex'
import {Notify } from 'vant'
export default defineComponent({
  setup() {
    const store = useStore()
    const imgCodeRef = ref()
    const state = reactive({
      username: 'H2102467',
      password: '1234',
      imgCode: ''
    })
    // 提交登录
    const onSubmit = () => {
      if(state.imgCode.toLocaleLowerCase() != imgCodeRef.value.imgCode.toLocaleLowerCase()){
        Notify({ 
          type: 'danger',
          message: '图形验证码错误' 
        });
        imgCodeRef.value.handleDraw()
        return;
      }
      store.dispatch('login',{
        userid: state.username,
        password: state.password,
      }).catch(res=>{
        imgCodeRef.value.handleDraw()
      })
    }
    return {
      ...toRefs(state),
      onSubmit,
      imgCodeRef
    }
  }
})
</script>

<style lang="scss">
  .login {
    .logo {
      width: 120px;
      height: 120px;
      display: block;
      margin: 80px auto 20px;
    }
    .login-body {
      padding: 0 20px;
    }
    .login {
      .link-register {
        font-size: 14px;
        margin-bottom: 20px;
        color: #1989fa;
        display: inline-block;
      }
    }
    .verify-bar-area {
      margin-top: 24px;
      .verify-left-bar {
        border-color: #1baeae;
      }
      .verify-move-block {
        background-color: #1baeae;
        color: #fff;
      }
    }
    .imgCode{
      display: block;
      width: 100px;
      height: 30px;
    }
    .verify {
      >div {
        width: 100%;
      }
      display: flex;
      justify-content: center;
      .cerify-code-panel {
        margin-top: 16px;
      }
      .verify-code {
        width: 40%!important;
        float: left!important;
      }
      .verify-code-area {
        float: left!important;
        width: 54%!important;
        margin-left: 14px!important;
        .varify-input-code {
          width: 90px;
          height: 38px!important;
          border: 1px solid #e9e9e9;
          padding-left: 10px;
          font-size: 16px;
        }
        .verify-change-area {
          line-height: 44px;
        }
      }
    }
  }
</style>
